<?php
	session_start();
	
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once('../../lib/'.'DBCommon.class.htm');
	require_once (EIU_CLASS.'BaseClass.htm');
	
	$today = date("Y-m-d h:i:s");
	$modifiedby = "";
	/*if(isset($_SESSION['userName']) && $_SESSION['userName']!=""){
		$modifiedby = $_SESSION['userName'];
	}*/
	
	if(isset($_POST['isDeleted']) && $_POST['isDeleted']==1)
	{
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		//update isDeleted
		$q_del_room = "Update `invoice_tbl` set `isDeleted`=1,`ModifiedBy`='".$_POST['modified']."' where `InvcID`=".$_POST['ID'];
		
		$rs_delete = $oDB->stl_db_change($q_del_room, $conn);
		echo $rs_delete;
		
		$oDB->stl_closeConn($conn);
	}
	
	if(isset($_POST['info']) && !empty($_POST['info']))
	{
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		
		$_POST['info'] = str_replace('\"', '"',$_POST['info']);
		$invc = json_decode($_POST['info'],true);	
		if(is_array($invc))
		{
			
			$invDate=changeDateFormat($invc['invcDate'],"words2num");
			if(empty($invc['invcID']))
			{
				//select invcNo
				$q="SELECT `InvcNo` FROM `invoice_tbl` WHERE `isDeleted`=0";
				$rs = $oDB->stl_db_select($q, $conn);
				if(is_array($rs))
				{
					foreach($rs as $key=>$value)
					{
						if($invc['invcNo']==$rs[$key]->InvcNo)
						{
							break;
						}
					}
				}
				//insert invc moi
				$q_insert = "Insert into `invoice_tbl`(`Type`,`InvcNo`,`SuppID`,`SuppName`,`VATAmount`,`BeforeVATAmount`,`AfterVATAmount`,`InvcDate`,`InvcImage`,`Description`,`isDeleted`,`CreateDate`,`ModifiedBy`,`input`)values(0,'".$invc['invcNo']."',".$invc['suppID'].",'".$invc['suppName']."',".$invc['VATAmount'].",".$invc['beforeVAT'].",".$invc['afterVAT'].",'".$invDate."','".$invc['image']."','".$invc['despt']."',0,now(),'".$invc['modified']."',0)";
				$rs_insert = $oDB->stl_db_change($q_insert, $conn);
				
				echo $rs_insert;
			}
			else			
			{
				
				//update cus_list_tbl
				$q_update="Update invoice_tbl set Type=0,InvcNo='".$invc['invcNo']."',SuppID=".$invc['suppID'].",SuppName='".$invc['suppName']."',`VATAmount`=".$invc['VATAmount'].",BeforeVATAmount=".$invc['beforeVAT'].",AfterVATAmount=".$invc['afterVAT'].",InvcDate='".$invDate."',Description ='".$invc['despt']."',`ModifiedBy`='".$invc['history']."' where InvcID=".$invc['invcID'];		
				//	print_r($q_update_customer);
				$rs_update = $oDB->stl_db_change($q_update, $conn);
				echo $rs_update;
			}
		}
		$oDB->stl_closeConn($conn);
	}
?>
